package com.back.network.web.dao;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.back.network.web.entity.User;

public interface UserTokenDao {

	@Insert("insert into user_token(userId,domain,token) values(#{user.id},#{user.domain},#{token})")
	void save(@Param(value = "user") User user, @Param(value = "token") String token);

	@Select("select domain from user_token where token=#{token}")
	String getDomain(String token);

	@Select("select count(1) from user_token where token=#{token} and DATE_ADD(creat_time, INTERVAL 30 DAY)>NOW()")
	int getCountByToken(String token);

	@Insert("update user_token set port =#{port} where token =#{token}")
	void saveUserPort(@Param(value = "token") String token,@Param(value = "port") int port);

	@Select("select port from user_token where domain = #{domain} order by update_time desc LIMIT 0,1")
	int getUserPortByDomain(String domain);

	@Select("select port from user_token where token=#{token}")
	int getUserPortByToken(String token);

	@Select("select token from user_token where port=#{port} ORDER BY update_time desc limit 0,1")
	String getTokenByPort(int port);
	
	@Select("select token from user_token where domain=#{domain} ORDER BY update_time desc limit 0,1")
	String getTokenByDomain(String domain);

}
